import { createApp } from 'vue'
import router from './router'
import store from './store'
import Antd, {notification} from 'ant-design-vue';
import App from './App.vue'
import 'ant-design-vue/dist/antd.css';
import * as Icons from '@ant-design/icons-vue';
import axios from 'axios';
import './assets/js/enums';

const app = createApp(App);
app.use(Antd).use(store).use(router).mount('#app');

//全局使用图标
const icons = Icons;
for (const i in icons) {
    app.component(i,icons[i]);
}

/**
 * axios拦截器
 */
axios.interceptors.request.use(function (config) {
    console.log('请求参数：', config);
    const _token = store.state.member.token;
    if (_token) {
        /**
         * String token = exchange.getRequest().getHeaders().getFirst("token");
         * 这里用的config.headers.token的token 要跟LoginMemberFilter中的token保持一致
         * @type {*|string|CancelToken}
         */
        config.headers.token = _token;
        console.log("请求headers增加token:", _token);
    }
    return config;
}, error => {
    return Promise.reject(error);
});
axios.interceptors.response.use(function (response) {
    console.log('返回结果：', response);
    return response;
}, error => {
    console.log('返回错误：', error);
    const response = error.response;
    /**
     * 这个地方是为了处理token过期的情况
     * 当token过期后 需要根据状态码对用户端进行信息的返回 同时需要将token信息重新置为0
     * 因为token过期后 页面校验会不通过 会变成401页面 也就重新跳回登录页面
     * @type {number | ((code: StatusCode) => (*|Response|null|T|AxiosResponse<T, D>|AxiosInterceptorManager<AxiosResponse>|AuthenticatorResponse))}
     */
    const status = response.status;
    if (status === 401) {
        // 判断状态码是401 跳转到登录页
        console.log("未登录或登录超时，跳到登录页");
        store.commit("setMember", {});
        notification.error({ description: "未登录或登录超时" });
        router.push('/login');
    }
    return Promise.reject(error);
});

axios.defaults.baseURL = process.env.VUE_APP_SERVER;
console.log('环境:',process.env.NODE_ENV)
console.log('服务端:',process.env.VUE_APP_SERVER)

